/*
 * @FileName: [RechargecardController.java]
 * 
 * @Package controller
 * 
 * 
 * Copyright (c) 2011-2015 BCINFO Technology Limited Com. All rights reserved.
 * 
 * This software is the confidential and proprietary information of BCINFO Technology Limited Company ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only in accordance with the terms of the contract agreement you entered into with RKY.
 * 
 * $Rev: 1013 $ $LastChangedDate: 2013-10-12 10:19:36 +0800 (星期六, 12 十月 2013) $ $LastChangedBy: xusp $
 * 
 * @category bcinfo-upp
 * 
 * @version 1.1
 * 
 * @author $Author: xusp $
 * 
 * Change History:[Formatter: author date description] <br/> 1 2 3
 */

package com.bcinfo.iccard.admin.iccard.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.bcinfo.iccard.admin.common.ModuleEnum;
import com.bcinfo.iccard.admin.controller.AbstractController;
import com.bcinfo.iccard.admin.iccard.entity.LabelValue;
import com.bcinfo.iccard.admin.util.TxtUtil;
import com.bcinfo.iccard.constant.RechargeableConstant;
import com.bcinfo.iccard.exception.CardNumIsExistException;
import com.bcinfo.iccard.exception.IccardDatabaseException;
import com.bcinfo.iccard.exception.IccardException;
import com.bcinfo.iccard.exception.RechargeableCardCancelPwdErrorException;
import com.bcinfo.iccard.exception.RechargeableCardCancelPwdNullException;
import com.bcinfo.iccard.exception.RechargeableCardIsNotExistByCardnumException;
import com.bcinfo.iccard.exception.RechargeableCardIsNotExistException;
import com.bcinfo.iccard.exception.RechargeableCardRechargeMoneyLessThanZeroException;
import com.bcinfo.iccard.exception.RechargeableCardRechargeMoneyNullException;
import com.bcinfo.iccard.exception.RechargeableCardRechargeTypeErrorException;
import com.bcinfo.iccard.exception.RechargeableCardStatusActivateException;
import com.bcinfo.iccard.exception.RechargeableCardStatusIsNotActivateException;
import com.bcinfo.iccard.exception.RechargeableCardUseNewPwdException;
import com.bcinfo.iccard.model.ExpenseRecord;
import com.bcinfo.iccard.model.RechargeableCard;
import com.bcinfo.iccard.model.RechargeableCardCancelDetail;
import com.bcinfo.iccard.model.RechargeableCardCreditDetail;
import com.bcinfo.iccard.model.RechargeableCardInfo;
import com.bcinfo.iccard.service.RechargeableCardService;
import com.bcinfo.iccard.util.PaginationList;
import com.bcinfo.upp.ac.model.Operator;
import com.bcinfo.upp.exception.UppDataBaseException;
import com.bcinfo.upp.exception.UppException;
import com.bcinfo.upp.util.AESCryptor;

/**
 * 充值卡controller <br/>
 * <p>
 * Description:<br/>
 * <p>
 * 包含以下功能 <br/>
 * <br/>
 * 
 * <PRE>
 *      /iccard/${actionName}
 * </PRE>
 * <p>
 */
@Controller
@RequestMapping("/iccard/rechargecard")
public class RechargecardController extends AbstractController
{
    @Autowired()
    private RechargeableCardService rechargeableCardService;

    /*
     * (non-Javadoc) Description: <br/>
     * 
     * @return
     * 
     * @see com.bcinfo.upp.admin.controller.AdminController#getMouduleCode()
     */
    public String getMouduleCode()
    {
        return ModuleEnum.RechargeableCard.moduleName.value;
    }

    /**
     * 转到RechargeableCard列表页面 <br/>
     * <p>
     * Description: 转到RechargeableCard列表页面 <br/>
     * <p>
     * Author: huangb<br/>
     * <p>
     * Date: 2013-4-12-下午2:56:22<br/>
     * <p>
     * 
     * @return
     * 
     */
    @RequestMapping("/toListView")
    public ModelAndView toListView()
    {
        String cardNum = request.getParameter("cardNum") == null ? "" : request.getParameter("cardNum");
        request.setAttribute("cardNum", cardNum);
        return new ModelAndView("iccard/recharge/rechargecard_list");
    }

    /**
     * 查询RechargeableCard列表 <br/>
     * <p>
     * Description: 查询RechargeableCard列表 <br/>
     * <p>
     * Author: huangb<br/>
     * <p>
     * Date: 2013-4-12-下午2:57:52<br/>
     * <p>
     * 
     * @return
     * 
     */
    @RequestMapping("/list")
    @ResponseBody
    public Map<String, Object> list(String cardNum, int status)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        PaginationList<RechargeableCardInfo> list = null;

        try
        {
            list = rechargeableCardService.find(cardNum, status, getPageSize(), getPage());
            if (list != null)
            {
                map.put("rows", list.getRecords());
                map.put("total", list.getTotalRecord());
            }
        }
        catch (IccardException e)
        {
            map.put("total", 0);
            handleException(e);
        }
        catch (Exception e)
        {
            map.put("total", 0);
            handleException(e);
        }

        return map;
    }

    /**
     * 查询RechargeableCard列表 <br/>
     * <p>
     * Description: 查询RechargeableCard列表 <br/>
     * <p>
     * Author: huangb<br/>
     * <p>
     * Date: 2013-4-12-下午2:57:52<br/>
     * <p>
     * 
     * @return
     * 
     */
    @RequestMapping("/inlist")
    @ResponseBody
    public List<RechargeableCard> inlist(String cardNum, int status)
    {
        List<RechargeableCard> list = null;
        try
        {
            list = rechargeableCardService.findInList(cardNum, status);
        }
        catch (IccardException e)
        {
            e.printStackTrace();
            handleException(e);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            handleException(e);
        }
        return list;
    }

    /**
     * 查询RechargeableCard列表 <br/>
     * <p>
     * Description: 查询RechargeableCard列表 <br/>
     * <p>
     * Author: huangb<br/>
     * <p>
     * Date: 2013-4-12-下午2:57:52<br/>
     * <p>
     * 
     * @return
     * 
     */
    @RequestMapping("/oplist")
    @ResponseBody
    public List<Operator> oplist(long roleId)
    {
        List<Operator> list = null;

        try
        {
            list = rechargeableCardService.findUserByRoleId(roleId);
        }
        catch (IccardException e)
        {
            e.printStackTrace();
            handleException(e);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            handleException(e);
        }
        return list;
    }

    /**
     * 创建RechargeableCard信息 <br/>
     * <p>
     * Description: 创建RechargeableCard信息 <br/>
     * <p>
     * Author: huangb<br/>
     * <p>
     * Date: 2013-4-12-下午2:58:32<br/>
     * <p>
     * 
     * @param request
     * @param response
     * @return
     * 
     */
    @RequestMapping("/add")
    @ResponseBody
    public Map<String, String> add(String cardNum)
    {
        // if (!this.hasPermission(ModuleEnum.RechargeableCard.add))// 没有权限
        // {
        // _log.error("权限不足");
        // return packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, null, "权限不足");
        // }
        Map<String, String> map = null;
        try
        {
            RechargeableCard rechargeableCard = new RechargeableCard();
            rechargeableCard.setRechargeableCardNum(cardNum);
            rechargeableCard.setRechargeableCardPwd(AESCryptor.encrypt(getConfigByKey("pwd")));
            rechargeableCard.setRechargeableCardStatus((short) 0);
            rechargeableCard.setRechargeableCardType((short) 0);
            rechargeableCard.setRechargeableCardAccountTotal((float) 0);
            rechargeableCardService.add(rechargeableCard, getOperatorId());

            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS, ERROR_VALUE_MSG_TYPE_INFO,
                    ERROR_VALUE_OP_RESULT_SUCCESS_NAME, null);
            addLog(ModuleEnum.RechargeableCard.add, "RechargeableCardNum=" + rechargeableCard.getRechargeableCardNum()
                    + "|desc=新增充值卡成功");
        }
        catch (CardNumIsExistException e)
        {
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, e.getClass()
                    .getSimpleName(), "充值卡号已存在，新增充值卡失败。");
            // addLog(ModuleEnum.RechargeableCard.add, "充值卡号已存在，新增充值卡失败。");
        }
        catch (IccardDatabaseException e)
        {
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, e.getClass()
                    .getSimpleName(), "数据库错误");
            // addLog(ModuleEnum.RechargeableCard.add, "数据库错误");
        }
        catch (Exception e)
        {
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, e.getClass()
                    .getSimpleName(), "新增充值卡失败");
            // addLog(ModuleEnum.RechargeableCard.add, "新增充值卡失败");
        }
        return map;
    }

    /**
     * 创建RechargeableCard信息 <br/>
     * <p>
     * Description: 创建RechargeableCard信息 <br/>
     * <p>
     * Author: huangb<br/>
     * <p>
     * Date: 2013-4-12-下午2:58:32<br/>
     * <p>
     * 
     * @param request
     * @param response
     * @return
     * 
     */
    @RequestMapping("/batchAdd")
    @ResponseBody
    public Map<String, String> batchAdd(String cardsFile)
    {
        // String cardsFile = request.getParameter("cardsFile");

        Map<String, String> map = null;
        try
        {
            List<String> result = new ArrayList<String>();
            result = TxtUtil.readTxtFile(cardsFile);
            for (int i = 0; i < result.size(); i++)
            {
                String strs = result.get(i);
            }
            List<String> errorCardNums = rechargeableCardService.batchAdd(result, getOperatorId());

            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS, ERROR_VALUE_MSG_TYPE_INFO,
                    ERROR_VALUE_OP_RESULT_SUCCESS_NAME, null);
            map.put("errorCardNums", errorCardNums.toString());
            addLog(ModuleEnum.RechargeableCard.add, "cardNums=" + result.toString() + "|desc=新增充值卡成功");
        }
        catch (IccardDatabaseException e)
        {
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, e.getClass()
                    .getSimpleName(), "数据库错误");
            // addLog(ModuleEnum.RechargeableCard.add, "数据库错误");
        }
        catch (Exception e)
        {
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, e.getClass()
                    .getSimpleName(), "新增充值卡失败");
            // addLog(ModuleEnum.RechargeableCard.add, "新增充值卡失败");
        }
        return map;
    }

    /**
     * 转到新增页面 <br/>
     * <p>
     * Description: 转到新增页面 <br/>
     * <p>
     * Author: huangb<br/>
     * <p>
     * Date: 2013-4-17-上午11:30:01<br/>
     * <p>
     * 
     * @return ModelAndView
     * 
     */
    @RequestMapping("/toAddView")
    public ModelAndView toAddView()
    {
        return new ModelAndView("iccard/recharge/rechargecard_add");
    }

    /**
     * 删除充值卡 <br/>
     * <p>
     * Description: 删除充值卡 <br/>
     * <p>
     * Author: huangb<br/>
     * <p>
     * Date: 2013-4-18-下午2:09:07<br/>
     * <p>
     * 
     * @param rechargeableCard
     * @return
     * 
     */
    @RequestMapping("/remove")
    @ResponseBody
    public Map<String, String> remove(RechargeableCard rechargeableCard)
    {
        // if (!this.hasPermission(ModuleEnum.RechargeableCard.remove))// 没有权限
        // {
        // _log.error("权限不足");
        // return packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, null, "权限不足");
        // }
        Map<String, String> map = null;
        if (rechargeableCard == null || rechargeableCard.getRechargeableCardId() <= 0)
        {
            return packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR,
                    "InvalidParameterException", null);
        }
        try
        {
            rechargeableCardService.delete(rechargeableCard.getRechargeableCardId(), getOperatorId());
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS, ERROR_VALUE_MSG_TYPE_INFO,
                    ERROR_VALUE_OP_RESULT_SUCCESS_NAME, null);
            addLog(ModuleEnum.RechargeableCard.remove, "RechargeableCardId=" + rechargeableCard.getRechargeableCardId()
                    + "|desc=删除充值卡成功");
        }
        catch (IccardDatabaseException e)
        {
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, e.getClass()
                    .getSimpleName(), "数据库错误");
            // addLog(ModuleEnum.RechargeableCard.add, "数据库错误");
        }
        catch (Exception e)
        {
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, e.getClass()
                    .getSimpleName(), null);
            // addLog(ModuleEnum.RechargeableCard.remove, "删除充值卡失败");
        }
        return map;
    }

    /**
     * 批量删除充值卡 <br/>
     * <p>
     * Description: 删除充值卡 <br/>
     * <p>
     * Author: huangb<br/>
     * <p>
     * Date: 2013-4-18-下午2:09:07<br/>
     * <p>
     * 
     * @param rechargeableCard
     * @return
     * 
     */
    @RequestMapping("/removeCard")
    @ResponseBody
    public Map<String, String> removeCard(long[] cardIds)
    {
        // if (!this.hasPermission(ModuleEnum.RechargeableCard.remove))// 没有权限
        // {
        // _log.error("权限不足");
        // return packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, null, "权限不足");
        // }

        Map<String, String> map = null;
        if (cardIds == null || cardIds.length <= 0)
        {
            return packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR,
                    "InvalidParameterException", "删除充值卡失败");
        }

        try
        {
            rechargeableCardService.batchDelete(cardIds, getOperatorId());
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS, ERROR_VALUE_MSG_TYPE_INFO,
                    ERROR_VALUE_OP_RESULT_SUCCESS_NAME, null);
            addLog(ModuleEnum.RechargeableCard.remove, "RechargeableCardId=" + cardIds.toString() + "|desc=删除充值卡成功");
        }
        catch (IccardDatabaseException e)
        {
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, e.getClass()
                    .getSimpleName(), "数据库错误");
            // addLog(ModuleEnum.RechargeableCard.add, "数据库错误");
        }
        catch (Exception e)
        {
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, e.getClass()
                    .getSimpleName(), null);
            // addLog(ModuleEnum.RechargeableCard.remove, "删除充值卡失败");
        }
        return map;
    }

    /**
     * 出库 <br/>
     * <p>
     * Description: <br/>
     * <p>
     * Author: huangb<br/>
     * <p>
     * Date: 2013-4-18-下午2:09:07<br/>
     * <p>
     * 
     * @param rechargeableCard
     * @return
     * 
     */
    @RequestMapping("/outbound")
    @ResponseBody
    public Map<String, String> outbound(long[] cardIds, long receiverId)
    {
        // if (!this.hasPermission(ModuleEnum.RechargeableCard.remove))// 没有权限
        // {
        // _log.error("权限不足");
        // return packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, null, "权限不足");
        // }

        Map<String, String> map = null;
        if (cardIds == null || cardIds.length <= 0)
        {
            return packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR,
                    "InvalidParameterException", "卡出库失败");
        }
        try
        {
            rechargeableCardService.batchOutbound(cardIds, getOperatorId(), receiverId);
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS, ERROR_VALUE_MSG_TYPE_INFO,
                    ERROR_VALUE_OP_RESULT_SUCCESS_NAME, null);
            addLog(ModuleEnum.RechargeableCard.remove, "RechargeableCardId=" + cardIds.toString() + "|desc=充值卡出库成功");
        }
        catch (IccardDatabaseException e)
        {
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, e.getClass()
                    .getSimpleName(), "数据库错误");
            // addLog(ModuleEnum.RechargeableCard.add, "数据库错误");
        }
        catch (Exception e)
        {
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, e.getClass()
                    .getSimpleName(), null);
            // addLog(ModuleEnum.RechargeableCard.remove, "卡出库失败");
        }
        return map;
    }

    /**
     * 
     * 卡激活 <br/>
     * <p>
     * Description: TODO(详细描述) <br/>
     * <p>
     * Author: Administrator<br/>
     * <p>
     * Date: 2013-12-9-上午11:30:00<br/>
     * <p>
     * 
     * @param cardNum
     * @return
     * @throws UppDataBaseException
     * 
     */
    @RequestMapping("/activate")
    @ResponseBody
    public Map<String, String> activate(String cardNum, Short rechargeType, Float rechargeMoney, String pwd)
            throws UppDataBaseException
    {
        // if (!this.hasPermission(ModuleEnum.RechargeableCard.add))// 没有权限
        // {
        // _log.error("权限不足");
        // return packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, null, "权限不足");
        // }
        Map<String, String> map = null;
        try
        {
            rechargeableCardService.activate(getOperatorId(), cardNum, rechargeType, rechargeMoney,
                    AESCryptor.encrypt(pwd));
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS, ERROR_VALUE_MSG_TYPE_INFO,
                    ERROR_VALUE_OP_RESULT_SUCCESS_NAME, null);
            addLog(ModuleEnum.RechargeableCard.activate, "RechargeableCardNum=" + cardNum + "|desc=卡激活成功");
        }
        catch (UppException e)
        {
            e.printStackTrace();
        }
        catch (RechargeableCardRechargeTypeErrorException e)
        {
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, e.getClass()
                    .getSimpleName(), "请选择正确的充值方式");
            addLog(ModuleEnum.RechargeableCard.activate, "请选择正确的充值方式");
        }
        catch (RechargeableCardIsNotExistByCardnumException e)
        {
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, e.getClass()
                    .getSimpleName(), "卡不存在");
            addLog(ModuleEnum.RechargeableCard.activate, "卡不存在");
        }
        catch (RechargeableCardStatusActivateException e)
        {
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, e.getClass()
                    .getSimpleName(), "该卡号为已激活状态");
            addLog(ModuleEnum.RechargeableCard.activate, "该卡号为已激活状态");
        }
        catch (RechargeableCardRechargeMoneyNullException e)
        {
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, e.getClass()
                    .getSimpleName(), "余额为0时，充值金额不能为0");
            addLog(ModuleEnum.RechargeableCard.activate, "余额为0时，充值金额不能为0");
        }
        catch (RechargeableCardUseNewPwdException e)
        {
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, e.getClass()
                    .getSimpleName(), "充值金额大于1000，不能使用默认密码，请输入新密码");
            addLog(ModuleEnum.RechargeableCard.activate, "充值金额大于1000，不能使用默认密码，请输入新密码");
        }
        catch (IccardDatabaseException e)
        {
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, e.getClass()
                    .getSimpleName(), "数据库错误");
            addLog(ModuleEnum.RechargeableCard.activate, "数据库错误");
        }

        return map;
    }

    /**
     * 
     * 卡充值 <br/>
     * <p>
     * Description: TODO(详细描述) <br/>
     * <p>
     * Author: Administrator<br/>
     * <p>
     * Date: 2013-12-9-下午04:31:59<br/>
     * <p>
     * 
     * @param cardNum
     * @param rechargeType
     * @param rechargeMoney
     * @param pwd
     * @return
     * 
     */
    @RequestMapping("/recharge")
    @ResponseBody
    public Map<String, String> recharge(String cardNum, Short rechargeType, float rechargeMoney, String pwd)
    {
        // if (!this.hasPermission(ModuleEnum.RechargeableCard.add))// 没有权限
        // {
        // _log.error("权限不足");
        // return packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, null, "权限不足");
        // }
        Map<String, String> map = null;
        try
        {
            rechargeableCardService.recharge(getOperatorId(), cardNum, rechargeType, rechargeMoney,
                    AESCryptor.encrypt(pwd));
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS, ERROR_VALUE_MSG_TYPE_INFO,
                    ERROR_VALUE_OP_RESULT_SUCCESS_NAME, null);
            addLog(ModuleEnum.RechargeableCard.recharge, "RechargeableCardNum=" + cardNum + "|desc=卡充值成功");
        }
        catch (UppException e)
        {
            e.printStackTrace();
        }
        catch (RechargeableCardRechargeTypeErrorException e)
        {
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, e.getClass()
                    .getSimpleName(), "请选择正确的充值方式");
            addLog(ModuleEnum.RechargeableCard.recharge, "请选择正确的充值方式");
        }
        catch (IccardDatabaseException e)
        {
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, e.getClass()
                    .getSimpleName(), "数据库错误");
            addLog(ModuleEnum.RechargeableCard.recharge, "数据库错误");
        }
        catch (RechargeableCardRechargeMoneyLessThanZeroException e)
        {
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, e.getClass()
                    .getSimpleName(), "充值金额小于零，不能进行充值");
            addLog(ModuleEnum.RechargeableCard.recharge, "充值金额小于零，不能进行充值");
        }
        catch (RechargeableCardIsNotExistByCardnumException e)
        {
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, e.getClass()
                    .getSimpleName(), "卡号不存在");
            addLog(ModuleEnum.RechargeableCard.recharge, "卡号不存在");
        }
        catch (RechargeableCardStatusIsNotActivateException e)
        {
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, e.getClass()
                    .getSimpleName(), "该卡号未激活，不能进行充值");
            addLog(ModuleEnum.RechargeableCard.recharge, "该卡号未激活，不能进行充值");
        }
        catch (RechargeableCardUseNewPwdException e)
        {
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, e.getClass()
                    .getSimpleName(), "充值金额大于1000，不能使用默认密码，请输入新密码");
            addLog(ModuleEnum.RechargeableCard.recharge, "充值金额大于1000，不能使用默认密码，请输入新密码");
        }
        return map;
    }

    /**
     * 
     * chongzhikaxun <br/>
     * <p>
     * Description: TODO(详细描述) <br/>
     * <p>
     * Author: Administrator<br/>
     * <p>
     * Date: 2013-12-9-下午05:40:49<br/>
     * <p>
     * 
     * @param cardNum
     * @param status
     * @return
     * 
     */
    @RequestMapping("/rechargesearch")
    @ResponseBody
    public Map<String, Object> rechargesearch(Long userId, String cardNum, String beginTime, String endTime)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        PaginationList<RechargeableCardInfo> list = null;

        try
        {
            list = rechargeableCardService.findDetails(userId, cardNum, beginTime, endTime, getPageSize(), getPage());
            if (list != null)
            {
                map.put("rows", list.getRecords());
                map.put("total", list.getTotalRecord());
            }
        }
        catch (IccardException e)
        {
            map.put("total", 0);
            handleException(e);
        }
        catch (Exception e)
        {
            map.put("total", 0);
            handleException(e);
        }

        return map;
    }

    @ResponseBody
    @RequestMapping("/getSellers")
    public List<LabelValue> listSellers(String keyword, int maxRows)
    {
        List<LabelValue> list = new ArrayList<LabelValue>();
        if (StringUtils.isBlank(keyword))
            return list;
        try
        {
            List<Operator> sellers = rechargeableCardService.selectUserByRoleIdAndUserName(keyword, maxRows);
            for (Operator o : sellers)
            {
                list.add(new LabelValue(o.getUserName(), String.valueOf(o.getOperatorId())));
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        //
        return list;
    }

    @RequestMapping("/canceledCardInfo")
    @ResponseBody
    public Map<String, String> canceledCardInfo(String cardNum, String pwd)
    {

        Map<String, String> map = null;
        // if (!this.hasPermission(ModuleEnum.ComboCard.update))// 没有权限
        // {
        // mv.addObject("updateCode", ModuleEnum.ComboCard.update);
        // return null;
        // }
        RechargeableCardInfo info = null;
        try
        {
            info = rechargeableCardService.findByCardnum(cardNum, AESCryptor.encrypt(pwd));
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS, ERROR_VALUE_MSG_TYPE_INFO,
                    ERROR_VALUE_OP_RESULT_SUCCESS_NAME, null);
            session.setAttribute("info", info);
            addLog(ModuleEnum.RechargeableCard.cancelation, "RechargeableCardNum=" + cardNum + "|desc=卡撤销成功");
        }
        catch (UppException e)
        {
            e.printStackTrace();
        }
        catch (RechargeableCardCancelPwdNullException e)
        {
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, e.getClass()
                    .getSimpleName(), "输入密码不能为空");
            addLog(ModuleEnum.RechargeableCard.cancelation, "输入密码不能为空");
        }
        catch (RechargeableCardCancelPwdErrorException e)
        {
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, e.getClass()
                    .getSimpleName(), "密码输入错误");
            addLog(ModuleEnum.RechargeableCard.cancelation, "密码输入错误");
        }
        catch (RechargeableCardIsNotExistException e)
        {
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, e.getClass()
                    .getSimpleName(), "卡号不存在");
            addLog(ModuleEnum.RechargeableCard.cancelation, "卡号不存在");
        }
        catch (IccardDatabaseException e)
        {
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, e.getClass()
                    .getSimpleName(), "数据库异常");
            addLog(ModuleEnum.RechargeableCard.cancelation, "数据库异常");
        }

        return map;
    }

    @RequestMapping("/cancelation")
    @ResponseBody
    public Map<String, String> cancelation()
    {

        Map<String, String> map = null;
        // if (!this.hasPermission(ModuleEnum.ComboCard.update))// 没有权限
        // {
        // mv.addObject("updateCode", ModuleEnum.ComboCard.update);
        // return null;
        // }
        // request.getHeader()
        RechargeableCardInfo info = (RechargeableCardInfo) session.getAttribute("info");
        try
        {
            rechargeableCardService.cancellation(info, getOperatorId());

            String cardNum = request.getParameter("cardNum") == null ? "" : request.getParameter("cardNum");
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS, ERROR_VALUE_MSG_TYPE_INFO,
                    ERROR_VALUE_OP_RESULT_SUCCESS_NAME, null);
            addLog(ModuleEnum.RechargeableCard.cancelation, "RechargeableCardNum=" + cardNum + "|desc=卡充值成功");
        }
        catch (RechargeableCardCancelPwdNullException e)
        {
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, e.getClass()
                    .getSimpleName(), "输入密码不能为空");
            addLog(ModuleEnum.RechargeableCard.cancelation, "输入密码不能为空");
        }
        catch (IccardDatabaseException e)
        {
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, e.getClass()
                    .getSimpleName(), "数据库错误");
            addLog(ModuleEnum.RechargeableCard.cancelation, "数据库错误");
        }
        catch (RechargeableCardIsNotExistException e)
        {
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, e.getClass()
                    .getSimpleName(), "卡号不存在");
            addLog(ModuleEnum.RechargeableCard.cancelation, "卡号不存在");
        }

        return map;
    }

    @RequestMapping("/searchNormal")
    @ResponseBody
    public Map<String, Object> searchNormal(Long userId, String cardNum)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        PaginationList<RechargeableCardInfo> list = null;

        try
        {
            list = rechargeableCardService.findByStatus(userId, cardNum,
                    RechargeableConstant.RECHARGEABLE_CARD_STATUS_ACTIVATE, getPageSize(), getPage());
            if (list != null)
            {
                map.put("rows", list.getRecords());
                map.put("total", list.getTotalRecord());
            }
        }
        catch (IccardException e)
        {
            map.put("total", 0);
            handleException(e);
        }
        catch (Exception e)
        {
            map.put("total", 0);
            handleException(e);
        }

        return map;
    }

    @RequestMapping("/searchFrozen")
    @ResponseBody
    public Map<String, Object> searchFrozen(Long userId, String cardNum, String beginTime, String endTime)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        PaginationList<RechargeableCardInfo> list = null;

        try
        {
            list = rechargeableCardService.findByStatus(userId, cardNum,
                    RechargeableConstant.RECHARGEABLE_CARD_STATUS_FREEZE, getPageSize(), getPage());
            if (list != null)
            {
                map.put("rows", list.getRecords());
                map.put("total", list.getTotalRecord());
            }
        }
        catch (IccardException e)
        {
            map.put("total", 0);
            handleException(e);
        }
        catch (Exception e)
        {
            map.put("total", 0);
            handleException(e);
        }

        return map;
    }

    /**
     * 
     * 单卡查询 <br/>
     * <p>
     * Description: TODO(详细描述) <br/>
     * <p>
     * Author: Administrator<br/>
     * <p>
     * Date: 2013-12-13-上午11:56:30<br/>
     * <p>
     * 
     * @param cardNum
     * @param pwd
     * @return
     * 
     */
    @RequestMapping("/singlecardSearch")
    @ResponseBody
    public Map<String, String> singlecardSearch(String cardNum, String pwd)
    {

        Map<String, String> map = null;
        // if (!this.hasPermission(ModuleEnum.ComboCard.update))// 没有权限
        // {
        // mv.addObject("updateCode", ModuleEnum.ComboCard.update);
        // return null;
        // }
        RechargeableCardInfo info = null;
        try
        {
            info = rechargeableCardService.findByCardnum(cardNum, AESCryptor.encrypt(pwd));
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS, ERROR_VALUE_MSG_TYPE_INFO,
                    ERROR_VALUE_OP_RESULT_SUCCESS_NAME, null);
            session.setAttribute("info", info);
            addLog(ModuleEnum.RechargeableCard.singlecardSearch, "RechargeableCardNum=" + cardNum + "|desc=单卡查询");
        }
        catch (UppException e)
        {
            e.printStackTrace();
        }
        catch (RechargeableCardCancelPwdNullException e)
        {
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, e.getClass()
                    .getSimpleName(), "输入密码不能为空");
            addLog(ModuleEnum.RechargeableCard.singlecardSearch, "输入密码不能为空");
        }
        catch (RechargeableCardCancelPwdErrorException e)
        {
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, e.getClass()
                    .getSimpleName(), "密码输入错误");
            addLog(ModuleEnum.RechargeableCard.singlecardSearch, "密码输入错误");
        }
        catch (RechargeableCardIsNotExistException e)
        {
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, e.getClass()
                    .getSimpleName(), "卡号不存在");
            addLog(ModuleEnum.RechargeableCard.singlecardSearch, "卡号不存在");
        }
        catch (IccardDatabaseException e)
        {
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, e.getClass()
                    .getSimpleName(), "数据库异常");
            addLog(ModuleEnum.RechargeableCard.singlecardSearch, "数据库异常");
        }

        return map;
    }

    // @RequestMapping("/infoBySinglecardSearch")
    // @ResponseBody
    // public Map<String, String> infoBySinglecardSearch()
    // {
    //
    // Map<String, String> map = null;
    // // if (!this.hasPermission(ModuleEnum.ComboCard.update))// 没有权限
    // // {
    // // mv.addObject("updateCode", ModuleEnum.ComboCard.update);
    // // return null;
    // // }
    // // request.getHeader()
    // RechargeableCardInfo info = (RechargeableCardInfo) session.getAttribute("info");
    // try
    // {
    // rechargeableCardService.cancellation(info, getOperatorId());
    // String cardNum = request.getParameter("cardNum") == null ? "" : request.getParameter("cardNum");
    // map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS, ERROR_VALUE_MSG_TYPE_INFO,
    // ERROR_VALUE_OP_RESULT_SUCCESS_NAME, null);
    // addLog(ModuleEnum.RechargeableCard.cancelation, "RechargeableCardNum=" + cardNum + "|desc=卡充值成功");
    // }
    // catch (IccardDatabaseException e)
    // {
    // map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, e.getClass()
    // .getSimpleName(), "数据库错误");
    // addLog(ModuleEnum.RechargeableCard.cancelation, "数据库错误");
    // }
    // catch (RechargeableCardIsNotExistException e)
    // {
    // map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, e.getClass()
    // .getSimpleName(), "卡号不存在");
    // addLog(ModuleEnum.RechargeableCard.cancelation, "卡号不存在");
    // }
    //
    // return map;
    // }

    @RequestMapping("/singlecardConsumeRecords")
    @ResponseBody
    public Map<String, Object> singlecardConsumeRecords(String cardNum, String merchantName, String beginTime,
            String endTime)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        PaginationList<ExpenseRecord> list = null;

        try
        {
            RechargeableCardInfo info = (RechargeableCardInfo) session.getAttribute("info");
            if (null != info)
            {
                cardNum = info.getRechargeableCardNum();
            }
            list = rechargeableCardService.findConsumeRecords(cardNum, merchantName, beginTime, endTime, getPageSize(),
                    getPage());
            if (list != null)
            {
                map.put("rows", list.getRecords());
                map.put("total", list.getTotalRecord());
            }
        }
        catch (IccardException e)
        {
            map.put("total", 0);
            handleException(e);
        }
        catch (Exception e)
        {
            map.put("total", 0);
            handleException(e);
        }

        return map;
    }

    @RequestMapping("/singlecardRechargeRecords")
    @ResponseBody
    public Map<String, Object> singlecardRechargeRecords(String cardNum, Short rechargeType, String merchantName,
            String beginTime, String endTime)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        PaginationList<RechargeableCardInfo> list = null;

        try
        {
            RechargeableCardInfo info = (RechargeableCardInfo) session.getAttribute("info");
            if (null != info)
            {
                cardNum = info.getRechargeableCardNum();
            }
            list = rechargeableCardService.findRechargeRecords(cardNum, rechargeType, beginTime, endTime,
                    getPageSize(), getPage());
            if (list != null)
            {
                map.put("rows", list.getRecords());
                map.put("total", list.getTotalRecord());
            }
        }
        catch (IccardException e)
        {
            map.put("total", 0);
            handleException(e);
        }
        catch (Exception e)
        {
            map.put("total", 0);
            handleException(e);
        }

        return map;
    }

    @RequestMapping("/cancelSumByDay")
    @ResponseBody
    public Map<String, Object> cancelSumByDay(String beginTime, String endTime)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        PaginationList<RechargeableCardCreditDetail> list = null;

        try
        {
            list = rechargeableCardService.findCanceledSumByDay(beginTime, endTime, getPageSize(), getPage());
            if (list != null)
            {
                map.put("rows", list.getRecords());
                map.put("total", list.getTotalRecord());
            }
        }
        catch (IccardException e)
        {
            map.put("total", 0);
            handleException(e);
        }
        catch (Exception e)
        {
            map.put("total", 0);
            handleException(e);
        }

        return map;
    }

    // 退款总额
    @ResponseBody
    @RequestMapping("/totalCancelAmount")
    public Map<String, Object> totalCancelAmount(String beginTime, String endTime)
    {

        Map<String, Object> map = new HashMap<String, Object>();
        float totalCancelAmount = rechargeableCardService.findCancelSum(beginTime, endTime);
        map.put("totalCancelAmount", totalCancelAmount);
        return map;
    }

    @RequestMapping("/findCanceledDetailsList")
    @ResponseBody
    public Map<String, Object> findCanceledDetailsList(String cardNum, Long userId, String beginTime, String endTime)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        PaginationList<RechargeableCardCancelDetail> list = null;

        try
        {
            String time = request.getParameter("time") == null ? "" : request.getParameter("time");
            if (StringUtils.isNotBlank(time))
            {
                beginTime = time;
                endTime = time;
            }
            list = rechargeableCardService.findCanceledDetailsList(cardNum, userId, beginTime, endTime, getPageSize(),
                    getPage());
            if (list != null)
            {
                map.put("rows", list.getRecords());
                map.put("total", list.getTotalRecord());
            }
        }
        catch (IccardException e)
        {
            map.put("total", 0);
            handleException(e);
        }
        catch (Exception e)
        {
            map.put("total", 0);
            handleException(e);
        }

        return map;
    }

    @RequestMapping("/loadInfoByCardNum")
    @ResponseBody
    public Map<String, String> loadInfoByCardNum(String cardNum, String pwd)
    {

        // if (!this.hasPermission(ModuleEnum.ComboCard.update))// 没有权限
        // {
        // mv.addObject("updateCode", ModuleEnum.ComboCard.update);
        // return null;
        // }
        // request.getHeader()
        Map<String, String> map = new HashMap<String, String>(4);
        String _cardNum = "";
        String _amountTotal = "";
        String _status = "";
        String _type = "";
        String _pwd = "";
        String _pwdresult = "";
        String _pwd1result = "";
        try
        {
            RechargeableCard info = null;
            info = rechargeableCardService.findByCardNum(cardNum);
            _cardNum = info.getRechargeableCardNum();
            _amountTotal = String.valueOf(info.getRechargeableCardAccountTotal());
            _status = String.valueOf(info.getRechargeableCardStatus());
            _type = String.valueOf(info.getRechargeableCardType());
            _pwd = info.getRechargeableCardPwd();
            String getPwdBySys = getConfigByKey("pwd");
            // 交易密码等于系统默认密码
            if (AESCryptor.encrypt(getPwdBySys).equals(_pwd))
            {
                _pwdresult = "0";
            }
            else
            {
                _pwdresult = "1";
            }
            // 前端传递的密码不为空时，保存此值，进行js的验证,不建议返回密码回前端。不安全
            if (StringUtils.isNotBlank(pwd))
            {
                if (AESCryptor.encrypt(pwd).equals(_pwd))
                {
                    _pwd1result = "0";
                }
                else
                {
                    _pwd1result = "1";
                }
            }

        }
        catch (UppException e)
        {
            e.printStackTrace();
        }
        catch (RechargeableCardIsNotExistByCardnumException e)
        {
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, e.getClass()
                    .getSimpleName(), "卡号不存在");
            addLog(ModuleEnum.RechargeableCard.loadinfo, "卡号不存在");
        }
        catch (IccardDatabaseException e)
        {
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, e.getClass()
                    .getSimpleName(), "数据库异常");
            addLog(ModuleEnum.RechargeableCard.loadinfo, "数据库异常");
        }
        map.put("cardNum", _cardNum);
        map.put("amountTotal", _amountTotal);
        map.put("status", _status);
        map.put("type", _type);
        map.put("pwdresult", _pwdresult);
        map.put("pwd1result", _pwd1result);
        return map;
    }
}